package com.nm.orm.dao.impl;

import com.nm.commons.exception.SupperException;
import com.nm.orm.model.State;
import com.nm.orm.dao.MenuDao;
import com.nm.orm.pojo.MenuEntity;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by hewu on 2016/6/19 0019.
 */
@Repository
@Qualifier("menusDao")
public class MenuDaoImpl extends DaoImpl<MenuEntity, String> implements MenuDao {

    public MenuDaoImpl() {
        super(MenuEntity.class);
    }

    private static final String UPDATE_AT = "updatedAt";
    private static String STATUS = "state";

    public List<MenuEntity> findAll() throws SupperException {
        Criteria criteria = getCurrentSession(sessionFactory).
                createCriteria(MenuEntity.class).addOrder(Order.desc(UPDATE_AT))
                .add(Restrictions.eq(STATUS, State.ABLE.dbValue()));

        return criteria.list();
    }


}
